﻿#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <list>
#include "../../../NetEngine_Linux/NetEngine_Release/NetEngine_Include/XyRyNet_CommHdr.h"
#include "../../../NetEngine_Linux/NetEngine_Release/NetEngine_Include/XyRyNet_Types.h"
#include "../../../NetEngine_Linux/NetEngine_Release/NetEngine_Include/XyRyNet_ProtocolHdr.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine/NetEngine_Core/NetCore_Define.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine/NetEngine_Core/NetCore_Error.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine/NetEngine_OPenSsl/OPenSsl_Define.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine/NetEngine_OPenSsl/OPenSsl_Error.h"

//g++ -std=gnu++11 -Wall -g Test_SSLServer.cpp -o Test_SSLServer -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_Core -lNetEngine_BaseLib -lNetEngine_Core -Wl,-rpath=../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_Core,--disable-new-dtags

void __stdcall TCPSelect_Recv_Callback(SOCKET hSocket, LPCSTR lpszClientAddr, LPCSTR lpszMsgBuffer, int nMsgLen, LPVOID lParam)
{
    if (-1 == nMsgLen)
    {
        printf("recv_Login:%s\n", lpszClientAddr);
        TCHAR tszSubject[2048];
        TCHAR tszIssus[2048];
        TCHAR tszAlg[2048];

        memset(tszSubject, '\0', sizeof(tszSubject));
        memset(tszIssus, '\0', sizeof(tszIssus));
        memset(tszAlg, '\0', sizeof(tszAlg));

        OPenSsl_Server_Accept(hSocket, lpszClientAddr, tszSubject, tszIssus, tszAlg);
        printf("%s\n%s\n%s\n", tszSubject, tszIssus, tszAlg);
    }
    else if (-2 == nMsgLen)
    {
        int nLen = 2048;
        TCHAR tszMsgBuffer[2048];
        memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
        if (OPenSsl_Server_Recv(lpszClientAddr, tszMsgBuffer, &nLen))
        {
            printf("OPenSsl_Server_Recv:%s\n", tszMsgBuffer);
            OPenSsl_Server_SendMsg(lpszClientAddr, _T("hello"),5);
        }
    }
}
int main()
{
    //生成方式参考论坛
    LPCTSTR lpszCAFile = _T("./ca.crt");
    LPCTSTR lpszSrvFile = _T("./Server.crt");
    LPCTSTR lpszKeyFile = _T("./ServerPrivate.key");

    if (!OPenSsl_Server_Init(lpszCAFile, lpszSrvFile, lpszKeyFile))
    {
        printf("OPenSsl_Server_Init %lX\n", NetCore_GetLastError());
        return -1;
    }

    if (!NetCore_TCPSelect_ShortStart(55443, TCPSelect_Recv_Callback,NULL,TRUE))
    {
        printf("NetCore_TCPIocp_StartEx %lX\n", NetCore_GetLastError());
        return -1;
    }

    printf("ok\n");
    while (1)
    {
        Sleep(1000);
    }

    NetCore_TCPSelect_ShortDestroy();
    return 0;
}
